const app = getApp()
const API = require('../../../../utils/api')
const util = require('../../../../utils/util')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        gardenId:'',
        startTime: '2021-01-01',
        endTime: '',
        sign: '1', //标记 1 升 2降
        type: '1', //1面积 2单价 3月租金
        pageSize: 10,
        pageNum: 1,
        reachBottomShow: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //获取今天的时间
        let endTime = util.formatTime( new Date() ).split(' ')[0]

        this.setData({
            gardenId: options.id,
            endTime
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow:function(){
        this.get_back_garden_house_list()
    },

    /**
     * 筛选条件改变
     */
    tabChange:function(e){
        let type = e.currentTarget.id
        let sign = this.data.sign
        console.log('tab改变', type)
        if( this.data.type == type ){ 
            this.data.sign == 1 ? sign = 2 : sign = 1
        }else{
            sign == 1
        }

        this.setData({
            type,
            sign
        })
        this.get_back_garden_house_list()
    },

    /**
     * 触底事件
     */
    reachBottomShow:function(){
        if( this.data.reachBottomShow ){
            let pageNum = this.data.pageNum
            pageNum ++
            this.setData({
                pageNum
            })
            this.get_back_garden_house_list()
        }
    },

    /**
     * 事件 - 筛选条件 - 开始时间
     */
    endTimeChange:function(e){
        this.setData({
            endTime: e.detail.value
        })
        this.get_back_garden_house_list()
    },

    /**
     * 事件 - 筛选条件 - 结束时间
     */
    startTimeChange:function(e){
        this.setData({
            startTime: e.detail.value
        })
        this.get_back_garden_house_list()
    },

    /**
     * 接口 - 房源列表
     */
    get_back_garden_house_list:function(){
        let that = this
        wx.showLoading({
          title: '加载中',
        })
        API.get_back_garden_house_list({
            gardenId: that.data.gardenId,
            startTime: that.data.startTime,
            endTime: that.data.endTime,
            sign: that.data.sign,
            type: that.data.type,
            pageSize: that.data.pageSize,
            pageNum: that.data.pageNum,
        }).then( res => {
            if( res.code == 200 ){
                that.setData({
                    houseList: res.rows
                })
                if( res.rows.length >= 10 ){
                    that.setData({
                        reachBottomShow: true
                    })
                }else{
                    that.setData({
                        reachBottomShow: false
                    })
                }
            }else{
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
            }
        })
        setTimeout( () => {
            wx.hideLoading()
        }, 1500)
    },

    /**
     * 路由 - 编辑/新增 表单
     */
    edit:function(e){
        let id = e.currentTarget.id
        if( id ){
            wx.navigateTo({
              url: '/pages/mine/park-manage/house/edit?gardenId=' + this.data.gardenId + '&id=' + id,
            })
            return
        }
        wx.navigateTo({
          url: '/pages/mine/park-manage/house/edit?gardenId=' + this.data.gardenId,
        })
    },

    /**
     * 接口 - 删除
     */
    delete:function(e){
        let that = this
        let id = e.currentTarget.id
        wx.showModal({
          title: '警告',
          content: '您确定删除该房源吗',
          success (res) {
            if (res.confirm) {
                API.delete_park_house({
                    id: id
                }).then( res => {
                    if( res.code == 200 ){
                        wx.showToast({
                            title: '删除成功',
                        })
                        that.get_back_garden_house_list()
                    }else{
                        wx.showToast({
                            title: res.msg,
                            icon:'none'
                        })
                    }
                })
            }
          }
        })

        
    }
})